package cn.lisens.jpa.web;

import java.util.List;
import java.util.stream.Collectors;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;

import cn.lisens.jpa.domain.PersonBase;
import cn.lisens.jpa.service.PersonBaseService;

@RestController
@RequestMapping("/person")
@Validated
public class PersonBaseController {

	@Autowired
	private PersonBaseService personBaseService;
	
	@RequestMapping("save")
	public String save(@Validated PersonBase personBase) {
//		, BindingResult result
//		if (result.hasErrors()) {
//            List<String> errors = result.getAllErrors()
//                .stream()
//                .map(DefaultMessageSourceResolvable::getDefaultMessage)
//                .collect(Collectors.toList());
//            return errors.toString();
//        }
		PersonBase p = personBaseService.save(personBase);
		System.out.println(p);
		return "保存成功，"+p;
	}
	
	@RequestMapping("update")
	public String update(@NotNull(message="主键不能为空") Long id, PersonBase personBase) {
		PersonBase p = personBaseService.update(personBase);
		System.out.println(p);
		return "更新成功，"+p;
	}
	
	@RequestMapping("listData")
	public List<PersonBase> listData(PersonBase personBase) {
		return personBaseService.findList(personBase.getIdcard());
	}
}
